package com.yami.shop.user.common.service.impl;

import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.user.common.dao.UserGasAccountMapper;
import com.yami.shop.user.common.dto.UserGasAccountVO;
import com.yami.shop.user.common.dto.UserGasRecordVO;
import com.yami.shop.user.common.model.UserGasAccount;
import com.yami.shop.user.common.model.UserGasAccountExample;
import com.yami.shop.user.common.service.UserGasAccountService;
import com.yami.shop.user.common.service.UserGasRecordService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserGasAccountServiceImpl implements UserGasAccountService {

    @Autowired
    private UserGasAccountMapper userGasAccountMapper;

    @Autowired
    @Lazy
    private UserGasRecordService userGasRecordService;

    @Override
    public UserGasAccount getUserGasById(String userId) {
        UserGasAccountExample example = new UserGasAccountExample();
        UserGasAccountExample.Criteria cri = example.createCriteria();
        cri.andUserIdEqualTo(userId);
        UserGasAccount userGasAccount = new UserGasAccount();
        List<UserGasAccount> userGasAccounts = userGasAccountMapper.selectByExample(example);
        if(userGasAccounts.size()>0){
            userGasAccount = userGasAccounts.get(0);
        }
        return userGasAccount;
    }

    @Override
    public void saveGasNum(UserGasAccountVO userGasAccountVO) {
        UserGasAccount userGasById = this.getUserGasById(userGasAccountVO.getUserId());
        if(ObjectUtils.isNotEmpty(userGasById.getGasNum())){
            //不是空 修改
            UserGasAccount userGasAccount = new UserGasAccount();
            userGasAccount.setUpdateTime(new Date());
            userGasAccount.setGasNum(userGasById.getGasNum()+userGasAccountVO.getGasNum());
            UserGasAccountExample example = new UserGasAccountExample();
            UserGasAccountExample.Criteria cri = example.createCriteria();
            cri.andUserIdEqualTo(userGasAccountVO.getUserId());
            userGasAccountMapper.updateByExampleSelective(userGasAccount,example);
        }else{
            //添加
            UserGasAccount userGasAccount = new UserGasAccount();
            userGasAccount.setUserId(userGasAccountVO.getUserId());
            userGasAccount.setGasNum(userGasAccountVO.getGasNum());
            userGasAccount.setDtcAddress(userGasAccountVO.getDtcAddress());
            userGasAccount.setCreateTime(new Date());
            userGasAccountMapper.insertSelective(userGasAccount);
        }
    }

    @Override
    public void updateGasNumCut(Integer gasNum, String userId) {
        UserGasAccount userGasById = this.getUserGasById(userId);
        if(userGasById==null || userGasById.getGasNum()==null){
            throw new YamiShopBindException("yami.user.gas.not.enough");
        }
        if(userGasById.getGasNum() == 0){
            throw new YamiShopBindException("yami.user.gas.not.enough");
        }
        if(userGasById.getGasNum()-gasNum < 0){
            throw new YamiShopBindException("yami.user.gas.not.enough");
        }
        UserGasAccount userGasAccount = new UserGasAccount();
        userGasAccount.setGasNum(userGasById.getGasNum()-gasNum);
        userGasAccount.setUpdateTime(new Date());
        UserGasAccountExample example = new UserGasAccountExample();
        UserGasAccountExample.Criteria cri = example.createCriteria();
        cri.andUserIdEqualTo(userId);
        userGasAccountMapper.updateByExampleSelective(userGasAccount,example);
        UserGasRecordVO userGasAccountVO = new UserGasRecordVO();
        userGasAccountVO.setUserId(userId);
        userGasAccountVO.setRecordNum(gasNum);
        userGasAccountVO.setType(2);   //转增成功
        userGasRecordService.insertGasRecord(userGasAccountVO);
    }

    @Override
    public void updateGasNumAdd(Integer gasNum, String userId) {
        UserGasAccount userGasById = this.getUserGasById(userId);
        if(ObjectUtils.isEmpty(userGasById)){
            throw new YamiShopBindException("yami.user.gas.rebate.error");
        }
        UserGasAccount userGasAccount = new UserGasAccount();
        userGasAccount.setGasNum(userGasById.getGasNum()+gasNum);
        userGasAccount.setUpdateTime(new Date());
        UserGasAccountExample example = new UserGasAccountExample();
        UserGasAccountExample.Criteria cri = example.createCriteria();
        cri.andUserIdEqualTo(userId);
        userGasAccountMapper.updateByExampleSelective(userGasAccount,example);
        UserGasRecordVO userGasAccountVO = new UserGasRecordVO();
        userGasAccountVO.setUserId(userId);
        userGasAccountVO.setRecordNum(gasNum);
        userGasAccountVO.setType(3);   //转增失败
        userGasRecordService.insertGasRecord(userGasAccountVO);
    }
}
